Plotly Ternary Contour Plot without Color¶

We build on the previous notebook and remove color from the contour plots. Is it difficult to implement? We take the same example as before and remove the color. The API reference of the contour plot is available at https://plotly.com/python-api-reference/generated/plotly.figure_factory.create_ternary_contour.html You need to set the following parameters:

  • coloring="lines" to remove the color. This will color the lines of the contour plot, which is not what we want.
  • linecolor="black" to set the color of the lines to black. This is not the default color, that is why we need to set it explicitly.

By setting these parameters we style the lines of the contour plot the way we want. However, by setting the contours to lines instead of filled contours, a grid is added to the plot. We have to use a different API which is documented at https://plotly.com/python/reference/layout/ternary. This is the API for the layout of the ternary plot. We need to update the plotly figure with the following calls to the update_ternaries method of the figure:

  • fig.update_ternaries(aaxis_showgrid=False) to remove the grid from the a-axis.
  • fig.update_ternaries(baxis_showgrid=False) to remove the grid from the b-axis.
  • fig.update_ternaries(caxis_showgrid=False) to remove the grid from the c-axis.

The background of the plot is still colored. We need to remove the color from the background. This can be done with the following:

  • fig.update_ternaries(bgcolor="white") to set the background color to white.

We need to color the sides of the triagle black. This is not as straightforward as it seems. The sides of the triangle are not configurable with the API we have used above. Even the term sides is not correct. The sides are actually the axes of the triangle. To work around this, we need to add a scatter plot in line mode, which will draw the sides of the triangle. For this we need to use the graph objects API. The API reference is available at https://plotly.com/python-api-reference/plotly.graph_objects.html.

Conclusion¶

We have removed the color from the contour plot, colored the contour lines black, removed the grid from the plot, and colored the background white. However, we still need to color the sides of the triangle black. This is not as straightforward as it seems. For the sake of keeping this notebook simple and focused, we will not color the sides of the triangle black. We will do this in the next notebook using the graph objects API. We will probably need to use the graph objects API for the contour ternary plot as well, since the figure factory API does not seem to be flexible enough to do what we want.

In [ ]:
import plotly.figure_factory as ff
import numpy as np

Al = np.array([0.0, 0.0, 0.0, 0.0, 1.0 / 3, 1.0 / 3, 1.0 / 3, 2.0 / 3, 2.0 / 3, 1.0])
Cu = np.array([0.0, 1.0 / 3, 2.0 / 3, 1.0, 0.0, 1.0 / 3, 2.0 / 3, 0.0, 1.0 / 3, 0.0])
Y = 1 - Al - Cu
# synthetic data for mixing enthalpy
# See https://pycalphad.org/docs/latest/examples/TernaryExamples.html
enthalpy = (Al - 0.01) * Cu * (Al - 0.52) * (Cu - 0.48) * (Y - 1) ** 2
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
    linecolor="black",
)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
    linecolor="black",
)
fig.update_ternaries(aaxis_showgrid=False)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
    linecolor="black",
)
fig.update_ternaries(aaxis_showgrid=False)
fig.update_ternaries(baxis_showgrid=False)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
    linecolor="black",
)
fig.update_ternaries(aaxis_showgrid=False)
fig.update_ternaries(baxis_showgrid=False)
fig.update_ternaries(caxis_showgrid=False)
fig.show()
In [ ]:
fig = ff.create_ternary_contour(
    np.array([Al, Y, Cu]),
    enthalpy,
    pole_labels=["Al", "Y", "Cu"],
    interp_mode="cartesian",
    coloring="lines",
    linecolor="black",
)
fig.update_ternaries(aaxis_showgrid=False)
fig.update_ternaries(baxis_showgrid=False)
fig.update_ternaries(caxis_showgrid=False)
fig.update_ternaries(bgcolor="white")
fig.show()